<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use http\Exception;
use Illuminate\Http\Request;
use App\Logic\ArticleCategory as ArticleCategoryLogic;
use App\Http\Requests\ArticleCategory as ArticleCategoryRequest;
use App\Logic\Article as ArticleLogic;
use Illuminate\Support\Facades\Validator;
use Overtrue\LaravelPinyin\Facades\Pinyin;

class ArticleCategoryController extends Controller
{
    private $articleCategoryLogic;
    private $articleCategoryRequest;
    private $articleLogic;

    public function __construct()
    {
        $this->articleCategoryLogic = new ArticleCategoryLogic();
        $this->articleCategoryRequest = new ArticleCategoryRequest();
        $this->articleLogic = new ArticleLogic();
    }

    public function index()
    {
        return view('article_category.index');
    }

    public function list(Request $request)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $start = ($page - 1) * $limit;
        $where['cate_name'] = $request->input('name');
        $result = [
            'code' => 0,
            'msg' => '获取数据成功！',
            'count' => $this->articleCategoryLogic->count($where),
            'data' => $this->articleCategoryLogic->list((int)$start, (int)$limit, $where),
        ];
        return response()->json($result);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'新增失败！'];

            $data['cate_name'] = $request->post('name','');
            $data['cate_pinyin'] = implode('', Pinyin::name($data['cate_name']));
            $data['cate_status']= $request->post('status',0);
            $data['cate_sort'] = $request->post('sort',0);
            $validator = Validator::make($data, $this->articleCategoryRequest->rules(), $this->articleCategoryRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if(!$this->articleCategoryLogic->checkName($data['cate_name'])){
                $result['msg'] = '分类名称已经存在！';
                return response()->json($result);
            }

            if ($this->articleCategoryLogic->add($data)){
                $result['status'] = true;
                $result['msg'] = '新增成功！';
                return response()->json($result);
            }

            return response()->json($result);

        } else {
            return view('article_category.add');
        }
    }

    public function edit(Request $request, $id = 0)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'修改失败！'];

            $id= $request->post('id',0);
            $data['cate_name']= $request->post('name','');
            $data['cate_pinyin'] = implode('', Pinyin::name($data['cate_name']));
            $data['cate_status'] = $request->post('status',0);
            $data['cate_sort'] = $request->post('sort',0);

            if(($id < 1) || !($this->articleCategoryLogic->has((int)$id))){
                $result['msg'] = '请勿非法提交！';
                return response()->json($result);
            }

            $validator = Validator::make($data, $this->articleCategoryRequest->rules(), $this->articleCategoryRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if(!$this->articleCategoryLogic->checkName($data['cate_name'], (int)$id)){
                $result['msg'] = '分类名称已经存在！';
                return response()->json($result);
            }

            if ($this->articleCategoryLogic->update((int)$id, $data)){
                $result['status'] = true;
                $result['msg'] = '修改成功！';
                return response()->json($result);
            }

            return response()->json($result);
        } else {
            if ($id < 1) abort(403);
            $data = $this->articleCategoryLogic->find((int)$id);
            if (empty($data)) abort(404);
            return view('article_category.edit', ['data' => $data]);
        }
    }

    public function status(Request $request, $id = 0)
    {
        $data = ['status' => false, 'msg' => '状态设置失败！'];
        if ($id >= 1){
            if ($this->articleCategoryLogic->status((int)$id)){
                $data['status'] = true;
                $data['msg'] = '状态设置成功！';
                return response()->json($data);
            }
        }
        return response()->json($data);
    }

    public function delete(int $id)
    {
        $result = ['status'=>false, 'msg'=>'删除失败！'];
        if(($id < 1) || !($this->articleCategoryLogic->has((int)$id))){
            $result['msg'] = '请勿非法提交！';
            return response()->json($result);
        }

        $articleCount = $this->articleLogic->countByCateId((int)$id);
        if ($articleCount > 0){
            $result['msg'] = '该分类下有文章，不能删除！';
            return response()->json($result);
        }

        if ($this->articleCategoryLogic->delete((int)$id)){
            $result['status'] = true;
            $result['msg'] = '删除成功！';
            return response()->json($result);
        }
        return response()->json($result);
    }
}
